package com.awccis.common.utils;


import cn.jiguang.common.resp.APIConnectionException;
import cn.jiguang.common.resp.APIRequestException;
import cn.jpush.api.push.model.PushPayload.Builder;
import cn.jpush.api.JPushClient;
import cn.jpush.api.push.PushResult;
import cn.jpush.api.push.model.Message;
import cn.jpush.api.push.model.Options;
import cn.jpush.api.push.model.Platform;
import cn.jpush.api.push.model.PushPayload;
import cn.jpush.api.push.model.audience.Audience;
import cn.jpush.api.push.model.audience.AudienceTarget;
import cn.jpush.api.push.model.notification.AndroidNotification;
import cn.jpush.api.push.model.notification.IosAlert;
import cn.jpush.api.push.model.notification.IosNotification;
import cn.jpush.api.push.model.notification.Notification;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.Map;


public class MessagePushUtil {
    private static final String MASTER_SECRET = "517e50b25f4e1660580fa744";  //TODO(填写你的MASTER_SECRET)
    private static final String APP_KEY = "6f09883fc2f0e6659a9a2c7e";     //TODO(填写你的APP_KEY)
    private static Logger logger = LoggerFactory.getLogger(MessagePushUtil.class);
    private static Builder builder = PushPayload.newBuilder();

    /**
     * @param @param payload
     * @Title: sendPushTryCatch TODO（开始推送）
     * @Description: try catch 推送
     */
    public static void sendPushTryCatch(PushPayload payload) {
        JPushClient jPushClient = new JPushClient(MASTER_SECRET, APP_KEY);
        try {
            PushResult pushResult = jPushClient.sendPush(payload);
            logger.info("返回结果" + pushResult);
        } catch (APIConnectionException e) {
            logger.error("连接错误，稍后尝试" + e);
        } catch (APIRequestException e) {
            logger.error("极光推送内部错误", e);
            logger.info("网络请求状态" + e.getStatus());
            logger.info("错误状态码" + e.getErrorCode());
            logger.info("错误信息" + e.getErrorMessage());
            logger.info("信息ID" + e.getMsgId());
            logger.info("极光推送错误信息:" + e.getErrorMessage());
        }
    }


    /**
     * @param alias   推送别名
     * @param alert   推送标题
     * @param title 推送标题内容（推荐json格式）
     * @param message 推送消息
     * @return
     */
    public static PushPayload pushAndroidAndIosByAlias(String alias, String alert, String title,String message) {
        IosAlert iosAlert = IosAlert.newBuilder().setTitleAndBody(alert, null, title).build();
        return builder
                .setPlatform(Platform.android_ios())  //推送平台
                .setAudience(Audience.alias(alias))   //推送目标,这里指定进行别名推送
                .setNotification(Notification.newBuilder()
                        .setAlert(iosAlert)
                        .addPlatformNotification(
                                AndroidNotification.newBuilder()
                                        .build())
                        .addPlatformNotification(IosNotification.newBuilder()
                                .setSound("happy")
                                .setBadge(1)
                                .build())
                        .build())
                .setMessage(Message.content(message))
                .setOptions(
                        Options.newBuilder()
                                .setApnsProduction(true)//IOS推送環境、True 表示推送生产环境，False 表示要推送开发环境；
                                .setTimeToLive(0)   //推送当前用户不在线时，为该用户保留多长时间的离线消息，以便其上线时再次推送。默认 86400 （1 天），最长 10 天。设置为 0 表示不保留离线消息，只有推送当前在线的用户可以收到。
                                .build())
                .build();
    }
}
